import { useState } from 'react'

const useAntdTable = (originColumns: any[]) => {
  const [columns, setColumns] = useState<any[]>(
    originColumns.map((col: any, index: number) => ({
      ...col,
      onHeaderCell: (column: { width: any }) => ({
        width: column.width,
        onResize: handleResize(index)
      })
    }))
  )

  const handleResize =
    (index: number) =>
    (_e: any, { size }: any) => {
      setColumns((prev) => {
        const nextColumns = [...prev]
        nextColumns[index] = {
          ...nextColumns[index],
          width: size.width
        }
        return nextColumns
      })
    }

  return {
    columns
  }
}

export default useAntdTable
